package io.renren.modules.bs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.exception.RRException;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.bs.dao.BsHospitalDao;
import io.renren.modules.bs.entity.BsDepartmentEntity;
import io.renren.modules.bs.entity.BsHospitalEntity;
import io.renren.modules.bs.service.BsDepartmentService;
import io.renren.modules.bs.service.BsHospitalService;
import io.renren.modules.bs.vo.BsHospitalVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("bsHospitalService")
public class BsHospitalServiceImpl extends ServiceImpl<BsHospitalDao, BsHospitalEntity> implements BsHospitalService {

    @Autowired
    private BsDepartmentService departmentService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BsHospitalEntity> page = this.page(
                new Query<BsHospitalEntity>().getPage(params),
                new QueryWrapper<BsHospitalEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void prohibitOrEnable(Integer id, Integer isFrozen) {
        BsHospitalEntity hospitalEntity = this.getById(id);
        if(hospitalEntity == null){
            throw new RuntimeException("未找到该医院");
        }
        hospitalEntity.setIsFrozen(isFrozen);
        boolean update = this.updateById(hospitalEntity);
        if(!update){
            throw new RuntimeException("更新失败");
        }
    }

    @Override
    public List<BsHospitalEntity> list(Map<String, Object> params) {
        return list();
    }

    @Override
    public void saveModel(BsHospitalEntity bsHospital) {

        //查询名字是否存在
        QueryWrapper<BsHospitalEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("hospital_name", bsHospital.getHospitalName());
        BsHospitalEntity hospitalEntity = this.getOne(wrapper);
        if(hospitalEntity != null){
            throw new RuntimeException("医院名称重复");
        }
        boolean save = this.save(bsHospital);
        if(!save){
            throw new RuntimeException("保存失败");
        }
    }

    @Override
    public void updateModel(BsHospitalEntity bsHospital) {

        QueryWrapper<BsHospitalEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("hospital_name",bsHospital.getHospitalName());
        wrapper.ne("hospital_id",bsHospital.getHospitalId());
        BsHospitalEntity hospitalEntity = this.getOne(wrapper);
        if(hospitalEntity != null){
            throw new RuntimeException("医院名称重复");
        }
        boolean update = this.updateById(bsHospital);
        if(!update){
            throw new RuntimeException("更新失败");
        }
    }

    @Override
    public Integer getExtractableNumber(Long dataValue) {
        return this.baseMapper.getExtractableNumber(dataValue);
    }

    @Override
    public List<BsHospitalVo> getTreeData() {
        return this.baseMapper.getTreeData();
    }

    @Override
    public void removeModelsByIds(List<Long> asList) {

        List<BsDepartmentEntity> departmentEntities = this.departmentService.getListByHospitalIds(asList);
        if(departmentEntities.size()  > 0){
            throw new RRException("请先删除科室");
        }
        this.removeByIds(asList);
    }

}